package com.ybxy;

import java.util.ArrayList;
import java.util.Objects;
import java.util.Scanner;

public class StudenArrayList {

    public static void main(String[] args) {
        ArrayList<Student> StudentArrayList = new ArrayList<>();
        Student student1 = new Student("张三", 18, "1111","四川省成都市");
        Student student2 = new Student("李四", 19, "2222","四川省绵阳市");
        Student student3 = new Student("王五", 20, "3333","四川省德阳市");
        Student student4 = new Student("赵六", 21, "4444","四川省宜宾市");
        Student student5 = new Student("孙七", 22, "5555","四川省广安市");

        StudentArrayList.add(student1);
        StudentArrayList.add(student2);
        StudentArrayList.add(student3);
        StudentArrayList.add(student4);
        StudentArrayList.add(student5);

        while(true){
            System.out.println("-----欢迎来到学生信息管理系统-----");
            System.out.println("1.添加学生信息");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生信息");
            System.out.println("4.查询所有学生");
            System.out.println("5.退出");
            System.out.println("---------------------------------");
            Scanner scanner = new Scanner(System.in);

            String choice = scanner.nextLine();
            if (Objects.equals(choice, "1")){
                addStudent(StudentArrayList);
            }
            else if (Objects.equals(choice, "2")) {
                deleteStudent(StudentArrayList);
            }
            else if (Objects.equals(choice, "3")) {
                updateStudent( StudentArrayList);
            }
            else if(Objects.equals(choice, "4")){
                queryStudent(StudentArrayList);
            }
            else if (Objects.equals(choice, "5")){
                System.out.println("退出系统成功");
                break;
            }else {
                System.out.println("输入错误，请重新输入");
            }
        }
    }
    private static void deleteStudent(ArrayList<Student> StudentArrayList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的学生的学号：");
        String id = scanner.next();
        int index = getIndex(StudentArrayList, id);
        if (index==-1){
            System.out.println("没有此学生");
        }else {
            StudentArrayList.remove(index);
            System.out.println("删除成功");
        }
    }

    public static void addStudent( ArrayList<Student> StudentArrayList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学生姓名：");
        String name = scanner.next();
        System.out.println("请输入学生年龄：");
        int age = scanner.nextInt();
        System.out.println("请输入学生学号：");
        String id = scanner.next();
        System.out.println("请输入学生地址：");
        String address = scanner.next();

        Student student = new Student(name, age, id, address);
        StudentArrayList.add(student);
        System.out.println("学生信息添加成功");
    }

    public static int  getIndex(ArrayList<Student> list,String id) {
        for (int i = 0; i < list.size(); i++){
            if (list.get(i).getId().equals(id)){
                return i;
            }
        }
        return -1;
    }

    public static void updateStudent(ArrayList<Student> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要修改的学生的id");
        String id = scanner.next();
        int index = getIndex(list, id);
        if (index == -1){
            System.out.println("没有这个学生");
            return;
        }else {
            System.out.println("请输入修改后的学生信息");
            System.out.println("请输入修改后的学生姓名：");
            String name = scanner.next();
            System.out.println("请输入修改后的学生年龄：");
            int age = scanner.nextInt();
            System.out.println("请输入修改后的学生地址：");
            String address = scanner.next();
            list.set(index, new Student(name,age,id,address));
            System.out.println("修改成功");
        }
    }

    public static void queryStudent (ArrayList<Student> list) {
        System.out.println("姓名"+" ----- "+"年龄"+" ----- "+"学号"+" ------ "+"地址");
        for (Student student : list){
            System.out.println(student.getName()+" ----- "+student.getAge()+" ----- "+student.getId()+" ----- "+student.getAddress());
        }
    }
}
